﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using nyxwin.Main.Base.ViewModels;
using nyxwin.Data.Bookmarks;
using System.Collections.Generic;
using System.Linq;
using System.Collections.ObjectModel;
using System.Collections.Specialized;

namespace nyxwin.Main.ViewModel.SubModels
{
    public class BookMarkCategoryModel : BaseViewModel, IEnumerable<BookMarkModel>, INotifyCollectionChanged
    {
        public BookMarkCategoryModel(Bookmark bookMark)
        {
            this.CategoryId = bookMark.CategoryId.Value;
            this.Title = bookMark.Title;
            this._bookmarks = new List<BookMarkModel>();
        }

        public int CategoryId { get; private set; }

        private string _title;

        public string Title
        {
            get { return (_title ?? string.Empty).ToLower(); }
            set
            {
                _title = value;
                RaisePropertyChanged(() => Title);
            }
        }

        private bool _isExpanded;

        public bool IsExpanded
        {
            get { return _isExpanded; }
            set
            {
                _isExpanded = value;
                RaisePropertyChanged(() => IsExpanded);
                RaisePropertyChanged(() => BookMarks);
                RaiseCollectionChanged();
            }
        }

        private List<BookMarkModel> _bookmarks;

        public void SetBookMarks(List<Bookmark> bookmarks)
        {
            foreach (var item in _bookmarks)
            {
                item.Unload();
            }

            _bookmarks.Clear();

            if (bookmarks != null)
            {
                foreach (var book in bookmarks)
                {
                    _bookmarks.Add(new BookMarkModel(book));
                }
            }

            RaisePropertyChanged(() => BookMarks);
            RaiseCollectionChanged();
        }

        public IEnumerable<BookMarkModel> BookMarks
        {
            get
            {
                if (IsExpanded)
                    return _bookmarks;
                else
                    return _bookmarks.Where(x => x.Unread > 0);
            }
        }

        #region IEnumerable<BookMarkModel> Members

        public IEnumerator<BookMarkModel> GetEnumerator()
        {
            return BookMarks.GetEnumerator();
        }

        #endregion

        #region IEnumerable Members

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return BookMarks.GetEnumerator();
        }

        #endregion

        #region INotifyCollectionChanged Members

        public event NotifyCollectionChangedEventHandler CollectionChanged;



        private void RaiseCollectionChanged()
        {
            Dispatcher.BeginInvoke(() =>
            {
                if (CollectionChanged != null)
                {
                    CollectionChanged(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
                }
            });
        }

        #endregion
    }
}
